package com.dsl.service;

import com.dsl.dao.MessageMapper;
import com.dsl.entity.Message;
import com.dsl.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @Description 消息业务类
 * @Author dsl
 * @Date 2024/9/10 下午4:07
 * @Version 1.0
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    //获取全部会话
    public List<Message> getConversations(int userId, int offset, int limit){
        return messageMapper.getConversations(userId, offset, limit);
    }

    //获取会话数量
    public int getConversationsCount(int userId){
        return messageMapper.getConversationCount(userId);
    }

    //获取私信
    public List<Message> getLetters(String conversationId, int offset, int limit){
        return messageMapper.getLetters(conversationId, offset, limit);
    }

    //获取私信数量
    public int getLetterCount(String conversationId){
        return messageMapper.getLetterCount(conversationId);
    }

    //获取未读私信数量
    public int getLetterUnreadCount(int UserId, String conversationId){
        return messageMapper.getLetterUnreadCount(UserId, conversationId);
    }

    //新增消息
    public int addMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.addMessage(message);
    }

    //读消息,状态变为1已读
    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids, 1);
    }

    //删除私信,状态变为2删除
    public int deleteMessage(int id){
        return messageMapper.updateStatus(List.of(id), 2);
    }

    //查询某个主题下最新的通知
    public Message getLatestNotice(int userId, String topic){
        return messageMapper.getLatestNotice(userId, topic);
    }

    //查询某个主题下通知数量
    public int getNoticeCount(int userId, String topic){
        return messageMapper.getNoticeCount(userId, topic);
    }

    //查询未读的通知数量
    public int getNoticeUnreadCount(int userId, String topic){
        return messageMapper.getNoticeUnreadCount(userId, topic);
    }

    //查询某个主题包含的通知列表
    public List<Message> getNotices(int userId, String topic, int offset, int limit){
        return messageMapper.getNotices(userId, topic, offset, limit);
    }

}
